<?php
class ImagesController extends AppController {

	var $name = 'Images';
	var $components = array('Uploader.Uploader');
	function beforeFilter() {
        parent::beforeFilter();
		$this->Uploader->uploadDir = 'files/uploads/products/'; //thu muc chua file upload
        $this->Uploader->enableUpload = true;
        $this->Uploader->maxFileSize = '5M'; // quy dinh dung luong duoc upload len toi da la 2 Megabytes
        $this->Uploader->maxNameLength = 25;//do dai cua ten file
        $this->Uploader->tempDir = TMP;
        
    }
    function admin_view($id = null) {
		$this->Image->recursive = 0;  		
 		$slides = $this->Image->find('all',array('conditions'=>array('Image.products_id'=>$id))); 
 		$this->loadModel('Product');
		$product = $this->Product->find('first',array('fields'=>array('name'),'conditions'=>array('Product.id'=>$id)));
		$this->set(compact('slides','id','product'));	
	}
	function admin_add($pid = null) {		
			//debug($this->data);
			$data = array();			
			if(!empty($this->data)){				
				$upload = $this->Uploader->uploadAll(array('url1','url2','url3','url4','url5'));					
				for($i=1;$i<6;$i++){					
					if(isset($upload['url'.$i])){
						$data['Image']['url'] =  $upload['url'.$i]['name'];	
						$data['Image']['note'] =   $this->data['Image']['note'.$i];	
						$data['Image']['products_id'] =  $this->data['Image']['products_id'];	
						//debug($data);
						$this->Image->create();
						if ($this->Image->save($data)){								
							$this->Session->setFlash(__('Đã lưu.', true));					
							//debug($data);
						} else {
							$this->Session->setFlash(__('Có lỗi xảy ra. Hãy thử lại!', true));
						}						
					}					
				}			
				$this->redirect(array('controller' =>'products','action' => 'view',$this->data['Image']['products_id']));
				
			}
			$this->loadModel('Product');
			$product = $this->Product->find('first',array('fields'=>array('name','id'),'conditions'=>array('Product.id'=>$pid)));
			$this->set(compact('product'));
			//debug($product);
		}
	function _delimg($id = null){
			$files = $this->Image->find('list',array(
				'conditions'=>array('Image.id'=>$id),
				'fields'=>'url')
			);
			$file ='';
			$file = "files" . DS . "uploads" . DS. "products" . DS .$files[$id];
			if(is_file($file)){
				unlink($file);
			}
	}
	function admin_delete($id = NULL) {
		$img = $this->Image->find('first',array('conditions'=>array('Image.id'=>$id)));
		$p = $img['Product']['id'];
		//debug($img);
		if (!$id) {
			$this->Session->setFlash(__('Không tồn tại ảnh này !', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->_delimg($id);
		if ($this->Image->delete($id)) {
			$this->Session->setFlash(__('Đã xóa !', true));
			$this->redirect(array('controller'=>'products','action'=>'view',$p));
		}
		else{
		$this->Session->setFlash(__('Có lỗi sảy ra. Xóa không thành công !', true));
		$this->redirect(array('controller'=>'products','action'=>'view',$p));
		}
	}
	function admin_mutildelete($str = null){
        if($str){
            $arrid=explode(',',$str);
        }else{
    		$this->Session->setFlash(__('Có lỗi sảy ra. Xóa không thành công !', true));
    		$this->redirect(array('action' => 'index',$str));
        }
         foreach($arrid as $item){
			$this->_delimg($item);
    		$this->Img->delete($item);
         }
		$this->Session->setFlash(__('Đã xóa !', true));
		$this->redirect(array('action' => 'index',$str));
    }   
}
